// storage.ts

enum StorageType {
  l = 'localStorage',
  s = 'sessionStorage'
}

class MyStorage {
  storage: Storage
  constructor(type: StorageType) {
    this.storage = type === StorageType.l ? window.localStorage : window.sessionStorage
  }

  set(
    key: string,
    value: any
  ) {

    if (typeof value === 'object') {
      let data = JSON.stringify(value)
      this.storage.setItem(key, data)
    } else {
      this.storage.setItem(key, value)
    }
  }
  get(key: string) {
    let value = this.storage.getItem(key)

    if (value) {
      try {
        return JSON.parse(value)
      } catch (error) {
        return value
      }
    }
  }
  delete(key: string) {
    this.storage.removeItem(key)
  }
  clear() {
    this.storage.clear()
  }
}


const LStorage = new MyStorage(StorageType.l)
const SStorage = new MyStorage(StorageType.s)

export { LStorage, SStorage }

